
#ifndef RTCHANNELHTTPBASE_H
#define RTCHANNELHTTPBASE_H

#include "RtConnectionInterface.h"
#include "RtHttpInterface.h"
#include "RtHttpHead.h"

enum
{
	NORMAL_HTTP_WORK = 0,
	APPEND_HTTPHEAD_FOR_EACH,
	DELIVER_PARTIAL_DATA
};
class /*RT_API_EXPORT*/ CRtChannelHttpBase 
	: public IRtTransportSink
{
public:
	CRtChannelHttpBase(IRtTransport *aTransport);
	virtual ~CRtChannelHttpBase();

	// interface IRtTransportSink
	virtual void OnReceive(
		CRtMessageBlock &aData,
		IRtTransport *aTrptId,
		CRtTransportParameter *aPara = NULL);

	virtual void OnSend(
		IRtTransport *aTrptId,
		CRtTransportParameter *aPara = NULL);

	virtual void OnDisconnect(
		RtResult aReason,
		IRtTransport *aTrptId);

	virtual void OnResponseFinished();

protected:
	virtual RtResult ParserHttpProtocol_t(CRtMessageBlock &aData) = 0;
	virtual RtResult Close_t(RtResult aReason);

	RtResult SetContentLength_i(CRtHttpHeaderArray &aHead, CRtMessageBlock *aData);
	RtResult SendData_i(CRtMessageBlock &aData);

	RtResult SetOption_i(DWORD aCommand, LPVOID aArg);
	RtResult GetOption_i(DWORD aCommand, LPVOID aArg);

protected:
	CRtHttpRequestHead m_RequestHeader;
	CRtHttpResponseHead m_ResponseHeader;
	IRtTransport *m_pTransportId;
	IRtTransportSink *m_pSink;
	CRtAutoPtr<IRtTransport> m_pTransport;
	BOOL m_bIsFirstSend;
	CRtMessageBlock *m_pMbSendBuf;
	DWORD m_dwSendBufMaxLen;
	BOOL m_customContentLength; // whether or not upper layer sets content length himself. 
	CRtThread *m_pThreadUser;

	BOOL m_bStream;
	BOOL m_bFullResponse; 
	BOOL m_needOnSend;
};

#endif // !RTCHANNELHTTPBASE_H
